from operator import index

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import abstract_event_listener as Ec
import pytest,time,logging
import json
time = time.strftime("%Y%m%d%H%M%S",time.localtime())

# def read_json(file_path):
#     with open(file_path,"r")as f:
#         data=json.load(f)
#         return [(item("username"),item("password") for item in data)]


@pytest.fixture()
def driver():
    driver=webdriver.Edge()
    driver.get('https://www.jiuxian.com/')
    driver.maximize_window()
    time.sleep(3)
    yield driver
    time.sleep(3)
    driver.quit()

# @pytest.mark.parametrize("username,password",read_json("D:\gao1\PythonProject4\Zk2\jsonzkb.json"))
def test_jiuxian_gwc(driver):
    #点击去登录(鼠标事件)
    driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/a[1]').click()
    # a = driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]')
    # ActionChains(driver).click(a)
    time.sleep(3)
    #断言1：请登录：账号登录
    dy1=driver.find_element(By.XPATH,'//*[@id="loginform"]/div/div/div/div/div[1]/div[1]/a[1]').text
    print("断言1请登录：",dy1)
    #输入账号密码
    driver.find_element(By.NAME,'uname').send_keys('13381144084')
    driver.find_element(By.ID,'password').send_keys('grq200566++')


    # driver.find_element(By.XPATH,'//input[contains(@id,"ersName")]')
    # driver.find_element(By.XPATH,'//input[@class=text or @id=password]')


    #手动执行验证
    time.sleep(15)
    #点击登录按钮
    driver.find_element(By.XPATH,'/html/body/div/form/div/div/div/div/div[1]/div[2]/div[4]/input').click()
    time.sleep(5)
    #断言2：登陆成功：退出
    dy2=driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]').text
    print("断言2登陆成功：",dy2)
    #搜索框输入（键盘事件）
    sr=driver.find_element(By.ID,'wd')
    sr.send_keys('五粮液')
    sr.send_keys(Keys.ENTER)
    driver.implicitly_wait(10)
    #断言3:搜索成功：
    dy3=driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]').text
    print("断言3搜索成功：",dy3)
    #点击商品进入详情
    # WebDriverWait(driver).until(Ec.AbstractEventListener,driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img')).click
    driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
    time.sleep(3)
    # #获取元素
    # a=driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img')
    # print(a.size)
    # print(a.is_selected())
    #切换窗口
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(3)
    #断言4商品详情：第五代五粮液
    dy4=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/h1').text
    print("断言4：商品详情：",dy4)
    #点击加入购物车
    try:
        driver.find_element(By.ID,'addToCartForDetail').click()
    except Exception as e:
        driver.save_screenshot('time' + '.png')
    time.sleep(3)

    # #进入框架退出框架
    # driver.switch_to.frame(driver.find_element(By.XPATH,'//input[start-with(@id,"presaleDescriptionId")]'))
    # time.sleep(3)
    # #退出框架
    # driver.switch_to.default_content()
    # #弹窗弹出
    # alert=driver.switch_to.alert
    # alert.accept()

def test_jiuxian_sc(driver):
    # 点击去登录(鼠标事件)
    driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/a[1]').click()
    # a = driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]')
    # ActionChains(driver).click(a)
    # time.sleep(3)
    # 断言1：请登录：账号登录
    dy1 = driver.find_element(By.XPATH, '//*[@id="loginform"]/div/div/div/div/div[1]/div[1]/a[1]').text
    print("断言1请登录：", dy1)
    # 输入账号密码
    driver.find_element(By.NAME, 'uname').send_keys('13381144084')
    driver.find_element(By.ID, 'password').send_keys('grq200566++')
    # 手动执行验证
    time.sleep(15)
    # 点击登录按钮
    driver.find_element(By.XPATH, '/html/body/div/form/div/div/div/div/div[1]/div[2]/div[4]/input').click()
    time.sleep(5)
    # 断言2：登陆成功：退出
    dy2 = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/a[1]').text
    print("断言2登陆成功：", dy2)
    # 搜索框输入（键盘事件）
    sr = driver.find_element(By.ID, 'wd')
    sr.send_keys('五粮液')
    sr.send_keys(Keys.ENTER)
    time.sleep(5)
    # 断言3:搜索成功：
    dy3 = driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]').text
    print("断言3搜索成功：", dy3)
    # 点击商品进入详情
    driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
    time.sleep(3)
    # 切换窗口
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(3)
    # 断言4商品详情：第五代五粮液
    dy4 = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/h1').text
    print("断言4：商品详情：", dy4)
    # 点击收藏按钮
    driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[3]/div[2]/div[1]/a/i').click()
    time.sleep(3)



    # #选择数量（下拉菜单）
    # num=driver.find_element(By.ID,'province')
    # num=index(1)


    # #选择地址（下拉菜单）
    # province=driver.find_element(By.ID,'province')
    # province=index(1)
    #
    # #输入详细地址
    # driver.find_element(By.ID,'address').send_keys('南口南大街2号')
    # #输入手机
    # driver.find_element(By.ID,'iphone').send_keys('13381144084')

